package marek.barak.windows.main;

import java.io.IOException;

import marek.barak.classes.UsersHddDAO;
import marek.barak.interfaces.UsersDAO;
import marke.barak.windows.composit.AddActions;
import marke.barak.windows.composit.BrandChoose;
import marke.barak.windows.composit.addUsers;
import marke.barak.windows.composit.chooseUser;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;


public class main {
	public static  UsersDAO users;
	public static Composite c;
	public static ScrolledComposite sc;
	public static void setShellName(Shell s,String n){
		s.setText(n);
	}
	static{
		try{
		users = new UsersHddDAO();
		
		}catch(IOException e){
			MessageBox msg = new MessageBox(new Shell(),SWT.ICON_ERROR|SWT.ABORT);
			msg.setMessage(e.toString());
			msg.setText("Error");
			msg.open();
			System.exit(-1);
		}catch(ClassNotFoundException ex){
			MessageBox msg = new MessageBox(new Shell(),SWT.ICON_ERROR|SWT.ABORT);
			msg.setMessage(ex.toString());
			msg.setText("Error");
			msg.open();
			System.exit(-1);
		}catch(Exception e){
			MessageBox msg = new MessageBox(new Shell(),SWT.ICON_ERROR|SWT.ABORT);
			msg.setMessage(e.toString());
			msg.setText("Error");
			msg.open();
			System.exit(-1);
		}
	}
	
	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			main window = new main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(480, 400);
		try {
			shell.setText("Measurements, User: "+users.getCurentUser());
		} catch (IndexOutOfBoundsException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		shell.setLayout(null);
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
		mntmNewSubmenu.setText("Options");
		
		Menu menu_1 = new Menu(mntmNewSubmenu);
		mntmNewSubmenu.setMenu(menu_1);
		
		MenuItem mntmMeasurements = new MenuItem(menu_1, SWT.NONE);
		mntmMeasurements.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(c!=null)
					c.dispose();
				c = new BrandChoose(shell,SWT.NONE);
				c.setBounds(0,0,shell.getBounds().width,shell.getBounds().height);
			}
		});
		mntmMeasurements.setText("Measurements");
		
		MenuItem mntmRunReport = new MenuItem(menu_1, SWT.NONE);
		mntmRunReport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(c!=null)
					c.dispose();
			}
		});
		mntmRunReport.setText("Run Report");
		
		MenuItem mntmUsers = new MenuItem(menu, SWT.CASCADE);
		mntmUsers.setText("Users");
		
		Menu menu_2 = new Menu(mntmUsers);
		mntmUsers.setMenu(menu_2);
		
		MenuItem mntmAdd = new MenuItem(menu_2, SWT.NONE);
		mntmAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(c!=null)
					c.dispose();
				c = new addUsers(shell,SWT.None);
				c.setBounds(0,0,450,334);
				
			}
		});
		mntmAdd.setText("Add");
		
		MenuItem mntmChoose = new MenuItem(menu_2, SWT.NONE);
		mntmChoose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(c!=null)
					c.dispose();
				c=new chooseUser(shell,SWT.NONE);
				c.setBounds(0, 0, shell.getBounds().width,shell.getBounds().height);
			}
		});
		mntmChoose.setText("Choose");
		
		MenuItem mntmAdmin_1 = new MenuItem(menu, SWT.CASCADE);
		mntmAdmin_1.setText("Admin");
		
		Menu menu_3 = new Menu(mntmAdmin_1);
		mntmAdmin_1.setMenu(menu_3);
		
		MenuItem mntmEditActions = new MenuItem(menu_3, SWT.NONE);
		mntmEditActions.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(c!=null)
					c.dispose();
				c = new AddActions(shell,SWT.NONE);
				c.setBounds(0,0,600,500);
			}
		});
		mntmEditActions.setText("Edit Actions");

	}
}
